<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/9/11
 * Time: 14:49
 */
namespace app\api\controller;

use app\api\Common;
use app\model\GamePhase;
use app\model\GuessUserbet;
use app\model\GuessWinner;
use app\model\GuessJq;
use app\model\GuessWinnerRf;
use app\model\GuessZf;
use app\model\Levelinfo;
use app\model\MatchList;
use app\model\MatchLive;
use app\model\PhaseList;
use app\model\Userinfo;
use think\Db;
use think\Request;


class Guess extends Common{

    /**
     *  返回直播赛事的详情
     */
    public function match_live(){
        //is_end 1直播 | 2竞猜
        $match_id = Request::instance()->param('match_id');
        $match_details = MatchLive::where('id',$match_id)->find();

        return return_json(1,'获取数据成功',$match_details);
    }


    /**
     * 赛事竞猜详情  h5
     */
    public function guess_details(){
        //获取客户端类型 1为安卓
        $headers = request()->header();
        if(empty($headers['pt'])){
            $headers['pt'] = 0;
        }

        //获取赛事id(match_id) | 赛事状态id(is_end)
        $match_id = Request::instance()->param('match_id');
        $is_end  = Request::instance()->param('is_end');
        $uid = Request::instance()->param('uid');
        if(empty($uid)){
            $uid = 0;
        }
        //判断赛事状态  1为直播 | 2竞猜中
        if($is_end == 2){
            //竞猜中的比赛
            $guess_details = MatchLive::where('is_end',2)->where('id',$match_id)->select();
        }else{
            //直播状态的比赛,获取状态为竞猜的其他比赛
            $guess_details = MatchLive::where('is_end',2)->order('start_time')->paginate(3);
        }
        foreach ($guess_details as $k=>$v){
            $v['matcheName'] = MatchList::where('id',$v['bigMatch_id'])->value('match_name').PhaseList::where('id',$v['phase_id'])->value('phase_name'). $v['matcheName'];
            $v['time'] = date("H:i:s",$v['start_time']);
            $v['start_time']= date("m月d日",$v['start_time']).weekday($v['start_time']);

            //查询胜负赔率
            $v['sf_peilv'] = GuessWinner::where('match_id',$v['id'])->find();
            //查询让分赔率
            $v['rf_peilv'] = GuessWinnerRf::where('match_id',$v['id'])->find();
            //查询总得分赔率
            $v['zf_peilv'] = GuessZf::where('match_id',$v['id'])->find();
            //查询是都都进球赔率
            $v['jq_peilv'] = GuessJq::where('match_id',$v['id'])->find();
        }

        return view('',['match_details'=>$guess_details,'uid'=>$uid,'pt'=>$headers['pt']]);

    }

    /**
     * 用户投注
     */
    public function userbet(){

        //用户id(uid)|比赛id(match_id)|竞猜id(jc_id)|竞猜赔率id(jc_pl_id)|竞猜类型id(jc_type)|投注类型(tz_type)|用户下注金额(wager)
        $data = Request::instance()->param();

        //查询比赛竞猜是否存在 | 获取用户下注的竞猜赔率
        if($data['jc_type']==1){
            $guess = GuessWinner::where('id',$data['jc_id'])->where('match_id',$data['match_id'])->find();
            $jc_peilv = GuessWinner::where('id',$data['jc_id'])->where('match_id',$data['match_id'])->value($data['jc_pl_id'].'_peilv_sf');
        }
        if($data['jc_type']==2){
            $guess = GuessWinnerRf::where('id',$data['jc_id'])->where('match_id',$data['match_id'])->find();
            $jc_peilv = GuessWinnerRf::where('id',$data['jc_id'])->where('match_id',$data['match_id'])->value($data['jc_pl_id'].'_peilv_rang');
        }
        if($data['jc_type']==3){
            $guess = GuessZf::where('id',$data['jc_id'])->where('match_id',$data['match_id'])->find();
            $jc_peilv = GuessZf::where('id',$data['jc_id'])->where('match_id',$data['match_id'])->value($data['jc_pl_id'].'_peilv_zong');
        }
        if($data['jc_type']==4){
            $guess = GuessJq::where('id',$data['jc_id'])->where('match_id',$data['match_id'])->find();
            $jc_peilv = GuessJq::where('id',$data['jc_id'])->where('match_id',$data['match_id'])->value($data['jc_pl_id'].'_peilv_jin');
        }
        if(empty($guess)){
            return return_json(0,'下注失败,比赛id和竞猜id不匹配!');
        }
        $wager = intval($data['wager']);   //整形化用户输入的金额
        //获取用户余额
        $user = Userinfo::where('id',$data['uid'])->find();
        $recharge_amount = $user['recharge_amount'];     //用户最新充值金额
        $operation_amount = $user['operation_amount'];       //用户可操作金额
        $total_amount = intval($user['total_amount']);       //用户总金额

        if($wager > $total_amount){
            return return_json(0,'余额不足,请充值');
        }

        //查用户等级单个竞猜可以下注的最大限额
        $jc_limit = Levelinfo::where('memberLevel',$user['memberLevel'])->value('jc_limit');
        if($wager > $jc_limit){
            return return_json(0,'当前投注的竞猜类型已超过当前等级下注限额,请选择其他竞猜类型下注!');
        }

        //计算用户下注后的金额
        if($wager > $recharge_amount){
            $operation_amount = $operation_amount-($wager - $recharge_amount);
            $recharge_amount = 0;
        }else{
            $recharge_amount = $recharge_amount - $wager;
        }

        /**
         * 开启事物
         */
        Db::startTrans();
        try{

            //用户经验暂不操作

            //下注成功扣除用的的金额
            $user->operation_amount = $operation_amount;
            $user->recharge_amount = $recharge_amount;
            $user->total_amount = $total_amount-$data['wager'];
            if($user->save()){
                //插入用户收支记录
                operation($data['uid'],$wager,'单选竞猜',1,$total_amount);
            }
            //插入用户下注记录
            $status = GuessUserbet::create([
                'user_id'=>$data['uid'],
                'match_id'=>$data['match_id'],
                'jc_id'=>$data['jc_id'],
                'jc_pl_id'=>$data['jc_pl_id'],
                'jc_type'=>$data['jc_type'],
                'tz_type'=>1,
                'wager' =>$wager,
                'peilv'=>$jc_peilv,
                'ctime'=>time(),
            ]);
            /**
             * 提交事物
             */
            Db::commit();
            if($status){
                return return_json(1,'下注成功');
            }
        }catch (\Exception $e){
            /**
             * 事物回滚
             */
            Db::rollback();
            return_json(0,'下注失败');
        }
    }

}